// pages/login/login.js
var app = getApp()
const DB = wx.cloud.database().collection("User")
const DB2 = wx.cloud.database().collection("current")
Page({
      test:function(){
        app.globalData.test=2
      },
    currentAcc_register:function(){
        console.log("1"),
        DB.get().then(res => {
        app.globalData.currentActLoc=res.data.length,
        // console.log("app.globalData.currentActLoc"),
        // console.log(app.globalData.currentActLoc),
       // console.log(this.data.account),
        app.globalData.currentAccount=this.data.account,
        // console.log(" app.globalData.currentAccount"),
        // console.log( app.globalData.currentAccount),
        DB.where({
          account:app.globalData.currentAccount
        }).get().then(res1=>{
          app.globalData.currentAccountId=res1.data[0]._id
          console.log("  app.globalData.currentAccountId"),
          console.log(  app.globalData.currentAccountId)
        })
       
        //更新
        DB.where({
          account:app.globalData.currentAccount
        }).get().then(res1=>{
          console.log("1111111"),
          console.log(app.globalData.currentAccount),
          console.log(app.globalData.currentActLoc),
          console.log(res1.data[0]._id),
        DB2.doc('bf4a0bf261cbf1f3013a109a212af472').update({
          data:{
            currentAccount:res1.data[0].account,
            currentAccountId:res1.data[0]._id,
            currentAccountLoc:app.globalData.currentActLoc,
          },
          success(res) {
           console.log("更新成功",res)
         },
           fail(res){
             console.log("更新失败",res)
           }
        })
        wx.switchTab({	//注册成功后跳转页面
          url: "/pages/forum/forum"
        }) 
      })
      })
    },
    currentAcc_login:function()
    {
      // DB2.get(app.globalData.currentActLoc).then(res => {
        this.test(),
      // }),
      DB.get().then(res => {
        for (let i = 0; i < res.data.length; i++) {
          if(res.data[i].account==this.data.account)
          {
            app.globalData.currentActLoc=i,
            // console.log("login"),
            // console.log(this.data.account),
            app.globalData.currentAccount=this.data.account
            DB.where({
              account:app.globalData.currentAccount
            }).get().then(res1=>{
              app.globalData.currentAccountId=res1.data[0]._id
              console.log("  app.globalData.currentAccountId"),
              console.log(  app.globalData.currentAccountId)

            })
            DB.where({
              account:app.globalData.currentAccount
            }).get().then(res1=>{
            DB2.doc('bf4a0bf261cbf1f3013a109a212af472').update({
              data:{
                currentAccount:res1.data[0].account,
                currentAccountId:res1.data[0]._id,
                currentAccountLoc:app.globalData.currentActLoc,
              },
              success(res) {
               console.log("更新成功",res)
             },
               fail(res){
                 console.log("更新失败",res)
               }
            })
       wx.switchTab({  //登录成功后跳转页面
                url: "/pages/forum/forum",
              })
          })
          }
        } 
        // wx.showToast({
        //   title: "OK"
        // }) 
      })
    },
    reg:function(evt)//注册函数
  {
    const db = wx.cloud.database();
    const userCollection = db.collection("User");
    let flag = false  //表示账户是否存在,false为初始值

    userCollection.get({
      success: (res) => {
        let user = res.data;  //获取到的对象数组数据
        // console.log(user);
        for (let i = 0; i < user.length; i++) {  //遍历数据库对象集合
          if (this.data.account === user[i].account) { //账户已存在
            flag = true;
            break;
          }
        }
        if (flag === true) {  //账户已存在
          wx.showToast({
            title: '账号已注册！',
            icon: 'error',
            duration: 1500
          })
        }
        else {  //账户未注册
          userCollection.add({
            data:{
              account:this.data.account,
              pwd:this.data.pwd,
              likeschools:[false,false,false],
              likequestions:[],
              comments:[],
              num:1,
              AttendEvenID:[]
            }
          })
          wx.showToast({	//显示注册成功信息
            title: '注册成功！',
            icon: 'success',
            duration: 1500
          })
          this.currentAcc_register()
          // console.log(currentActLoc),
         
          // console.log("32425455",app.globalData.currentAccountId)
        }
      }
    })
  },
  login:function(evt)//登录函数
  {
    const db = wx.cloud.database();
    const userCollection = db.collection("User");
    let flag = false  //表示账户是否存在,false为初始值
    userCollection.get({
      success: (res) => {
        let user = res.data;
        console.log(user);
        for (let i = 0; i < user.length; i++) {  //遍历数据库对象集合
          if (this.data.account === user[i].account) { //账户已存在
            flag=true;
            if (this.data.pwd !== user[i].pwd) {  //判断密码正确与否
              wx.showToast({  //显示密码错误信息
                title: '密码错误！',
                icon: 'error',
                duration: 1500
              });
               i=user.length; //密码错误则重头开始遍历数据库对象集合
            } else {
              wx.showToast({  //显示登录成功信息
                title: '登录成功！',
                icon: 'success',
                duration: 1500
              })
              flag=true;
              this.currentAcc_login()
           
              // console.log("32425455",app.globalData.currentAccountId)
              break;
            }
          }
        };
        if(flag==false)//遍历完数据后发现没有该账户
        {
          wx.showToast({
            title: '用户不存在！',
            icon: 'error',
            duration: 1500
          })
        }
      }
    })
  },
  getAccount:function(evt)
  {
    this.setData({account:evt.detail.value});//将数据存进data
    console.log(this.data.account);
  },
  getPwd:function(evt)
  {
    this.setData({pwd:evt.detail.value});//将数据存进data
    console.log(this.data.pwd);
  }
})